<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    	<?php if(isset($breadcrumb['href']) && $breadcrumb['href'] != '' && $breadcrumb['href'] != '#') {?>
	    	<a href="<?php echo $breadcrumb['href']; ?>">
	    		<?php echo $breadcrumb['text']; ?>
	    	</a>
    	<?php }else{?>
    		<?php echo $breadcrumb['text']; ?>
    	<?php }?>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/template.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="location = '<?php echo $insert; ?>'" class="button"><?php echo $button_insert; ?></a><a onclick="$('form').submit();" class="button"><?php echo $button_delete; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
        <table class="list">
          <thead>
            <tr>
              <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
              <td class="center" style="width:104px"><?php echo $column_image; ?></td>
              <td class="left"><?php if ($sort == 't.name') { ?>
                <a href="<?php echo $sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_name; ?>"><?php echo $column_name; ?></a>
                <?php } ?></td>
              <td class="left" width="70px"><?php if ($sort == 't.type') { ?>
                <a href="<?php echo $sort_type; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_type; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_type; ?>"><?php echo $column_type; ?></a>
                <?php } ?></td>
              <td class="left" style="width:200px"><?php echo $column_restaurant; ?></td>
              <td class="center" style="width:100px"><?php if ($sort == 't.status') { ?>
                <a href="<?php echo $sort_status; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_status; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_status; ?>"><?php echo $column_status; ?></a>
                <?php } ?></td>
              <td class="center" style="width:100px"><?php echo $column_action; ?></td>
            </tr>
          </thead>
          <tbody>
            <tr class="filter">
              <td></td>
              <td></td>
              <td><input type="text" name="filter_name" value="<?php echo $filter_name; ?>" /></td>
              <td>
              <select name="filter_type">
                  <option value=""></option>
                  <?php if ($filter_type) { ?>
                  <option value="1" selected="selected"><?php echo $text_private; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_private; ?></option>
                  <?php } ?>
                  <?php if (!is_null($filter_type) && !$filter_type) { ?>
                  <option value="0" selected="selected"><?php echo $text_public; ?></option>
                  <?php } else { ?>
                  <option value="0"><?php echo $text_public; ?></option>
                  <?php } ?>
                </select>
              </td>
              <td align="left"><input type="text" name="filter_restaurant_name" value="<?php echo $filter_restaurant_name; ?>"/></td>
              <td align="center"><select name="filter_status">
                  <option value="*"></option>
                  <?php if ($filter_status) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <?php } ?>
                  <?php if (!is_null($filter_status) && !$filter_status) { ?>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
              <td align="center"><a onclick="filter();" class="button"><?php echo $button_filter; ?></a></td>
            </tr>
            <?php if ($templates) { ?>
            <?php foreach ($templates as $template) { ?>
            <tr>
              <td style="text-align: center;">
          	  	<?php 
          	  	$disable = "";
           		if($template['inUse']) {
           			$disable = "disabled='disabled'";
           		}
              	?>
              	<?php if ($template['selected']) { ?>
                <input <?php echo $disable;?> type="checkbox" name="selected[]" value="<?php echo $template['template_id']; ?>" checked="checked" />
                <?php } else { ?>
                <input <?php echo $disable;?> type="checkbox" name="selected[]" value="<?php echo $template['template_id']; ?>" />
                <?php } ?></td>
              <td class="center"><img src="<?php echo $template['image']; ?>" alt="<?php echo $template['name']; ?>" style="padding: 1px; border: 1px solid #DDDDDD;" /></td>
              <td class="left">
	              <?php echo $template['name']; ?>
	              <?php 
	              	if($template['inUse']) {
	              ?>
	              	<span class="error"><?php echo $text_in_use; ?></span>
	              <?php
	              	}
	              ?>
	              <?php
	              	if (isset($error_template_in_use[$template['template_id']])) { ?>
	              	<span class="error"><?php echo $error_template_in_use[$template['template_id']] ?></span>
	              <?php
	              	}
	              ?>
              </td>
              <td class="left"><?php echo $template['type']; ?></td>
              <td class="left"><?php echo $template['restaurant_name']; ?></td>
              <td class="center"><?php echo $template['status']; ?></td>
              <td class="center"><?php foreach ($template['action'] as $action) { ?>
                [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
                <?php } ?></td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="center" colspan="8"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </form>
      <div class="pagination"><?php echo $pagination; ?></div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
function filter() {
	url = 'index.php?route=setting/template&token=<?php echo $token; ?>';
	
	var filter_name = $('input[name=\'filter_name\']').attr('value');
	
	if (filter_name) {
		url += '&filter_name=' + encodeURIComponent(filter_name);
	}
	
	var filter_type = $('select[name=\'filter_type\']').attr('value');
	
	if (filter_type) {
		url += '&filter_type=' + encodeURIComponent(filter_type);
	}
	
	var filter_restaurant_name = $('input[name=\'filter_restaurant_name\']').attr('value');
	
	if (filter_restaurant_name) {
		url += '&filter_restaurant_name=' + encodeURIComponent(filter_restaurant_name);
	}
	
	var filter_status = $('select[name=\'filter_status\']').attr('value');
	
	if (filter_status != '*') {
		url += '&filter_status=' + encodeURIComponent(filter_status);
	}
	
	<?php if(isset($this->request->get['order'])){ ?>
		url += '&order=' + encodeURIComponent("<?php echo $this->request->get['order']; ?>");
	<?php } ?>
	
	<?php if(isset($this->request->get['sort'])){ ?>
		url += '&sort=' + encodeURIComponent("<?php echo $this->request->get['sort']; ?>");
	<?php } ?>

	location = url;
}
//--></script> 
<script type="text/javascript"><!--
$('#form input').keydown(function(e) {
	if (e.keyCode == 13) {
		filter();
	}
});
//--></script> 
<script type="text/javascript"><!--
$('input[name=\'filter_name\']').autocomplete({
	delay: 0,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=setting/autocomplete/template&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {		
				response($.map(json, function(item) {
					return {
						label: item.name,
						value: item.name
					};
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'filter_name\']').val(ui.item.label);
						
		return false;
	}
});

$('input[name=\'filter_restaurant_name\']').autocomplete({
	delay: 0,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=restaurant/autocomplete/restaurant&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item.name,
						value: item.name,
						url: item.url
					};
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'filter_restaurant_name\']').val(ui.item.label);
						
		return false;
	}
});
//--></script> 
<?php echo $footer; ?>